<?php
//自定义页面
namespace app\admin\controller;
use app\common\model\AdminRole;
use think\facade\Request;
use app\common\middleware\Auth;
use app\common\middleware\Log;
use app\common\model\AdminUser;
use think\facade\Cookie;

class Index
{
    protected $middleware = [Auth::class => ['except' => 'Login'], Log::class => []];

    public function Index()
    {
        return ViewTpl('/admin/index');
    }

    public function Login()
    {
        if (Request::isAjax() || Request::isPost()) {

            $AdminUser = new AdminUser();
            if (empty(input('user'))) {
                return BackCode([], '203', '用户名不能为空');
            }
            if (empty(input('upass'))) {
                return BackCode([], '203', '密码不能为空');
            }
            $user = $AdminUser->where("user='" . input('user') . "' and upass='" . md5(input('upass')) . "' and is_delete=0")->withoutField('upass')->find();
            if (empty($user)) {
                return BackCode([], '203', '账号或密码输入错误');
            }
            if ($user['status'] == 2) {
                return BackCode([], '203', '此账号已停用');
            }
            $user['login_source'] = 'admin';
            $baock['token'] = signToken($user, 3600 * 24);
            $baock['url'] = '/admin/';
            $baock['userinfo'] =$user;
            Cookie::set('token', $baock['token'], 3600 * 24);
            return BackCode($baock);
        }
        return ViewTpl('/admin/login');
    }


    public function Menu()
    {
        if (Request::isAjax() || Request::isPost()) {
            $power=new \app\common\service\Power;
            $data=$power->GetAllNode();
            $data['menu'] = GetTree($data['menu'], 'parent_id', 'children');
            return BackCode($data);
        }
    }

    public function Message()
    {
        if (Request::isAjax() || Request::isPost()) {
            $data = [
                ['id' => 1, 'title' => '通知', 'children' => [
                    [
                        'id' => 11,
                        'avatar' => 'https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png',
                        'title' => '你收到了 14 份新周报',
                        'context' => '这是消息内容',
                        'form' => '就眠仪式',
                        'time' => '刚刚',
                        'url'=>''
                    ],
                    [
                        'id' => 12,
                        'avatar' => 'https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png',
                        'title' => '你收到了 14 份新周报',
                        'context' => '这是消息内容',
                        'form' => '就眠仪式',
                        'time' => '刚刚',
                        'url'=>''
                    ],
                    [
                        'id' => 12,
                        'avatar' => 'https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png',
                        'title' => '你收到了 14 份新周报',
                        'context' => '这是消息内容',
                        'form' => '就眠仪式',
                        'time' => '刚刚',
                        'url'=>''
                    ],
                    [
                        'id' => 12,
                        'avatar' => 'https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png',
                        'title' => '你收到了 14 份新周报',
                        'context' => '这是消息内容',
                        'form' => '就眠仪式',
                        'time' => '刚刚',
                        'url'=>''
                    ],
                ]],
                ['id' => 2, 'title' => '消息', 'children' => []],
                ['id' => 3, 'title' => '待办', 'children' => []],
            ];
            return BackCode($data);
        }
    }

    public function EditPass()
    {
        if (Request::isAjax() || Request::isPost()) {
            $upass = input('upass');
            $ok_upass = input('ok_upass');
            if (empty($upass)) {
                return BackCode([], '203', '新密码不能为空');
            }
            if (strlen($upass) < 6) {
                return BackCode([], '203', '密码长度不得低于6位字符');
            }
            if (empty($ok_upass)) {
                return BackCode([], '203', '确定密码不能为空');
            }
            if ($ok_upass != $upass) {
                return BackCode([], '203', '两次输入的密码不一致');
            }
            $AdminUser = new AdminUser();
            $AdminUser->where("id='" . GetToken('id') . "' and is_delete=0")->update(['upass' => md5($upass)]);
            Cookie::set('token', '', 3600 * 24);
            return BackCode();
        }
    }


    public function SetTing()
    {
        if (Request::isAjax() || Request::isPost()) {
            $AdminRole = new AdminRole();
            $Role = $AdminRole->where("id='" . GetToken("role_ids") . "' and is_delete=0")->field("home")->find();
            $index = !empty($Role['home']) ? $Role['home'] : '/admin/workbench/index/';
            $title =  !empty($Role['home']) ? '首页' : '分析页';
            $id =!empty($Role['home']) ? '0' : '265';
            $logo_title = '欢迎您，' . GetToken('user') . '<br>' . GetConfig('system', 'system_name');
            $logo_image = GetConfig('system', 'system_logo');
            $logo_qrcode = '';
            $data = [
                'logo' => [
                    'title' => $logo_title,
                    'image' => $logo_image,
                    'qrcode' => $logo_qrcode
                ],
                'menu' => [
                    'data' => '/admin/index/menu/',
                    'method' => 'POST',
                    'accordion' => true,
                    'collapse' => false,
                    'control' => true,
                    'select' => $id,
                    'async' => true,
                ],
                'tab' => [
                    'enable' => true,
                    'keepState' => true,
                    'session' => false,
                    'preload' => false,
                    'max' => '30',
                    'index' => [
                        'id' => $id,
                        'href' => $index,
                        'title' => $title,
                    ],
                ],
                'theme' => [
                    'defaultColor' => '5',
                    'defaultMenu' => 'dark-theme',
                    'defaultHeader' => 'light-theme',
                    'allowCustom' => true,
                    'banner' => false,
                ],
                'colors' => [
                    ['id' => '1', 'color' => '#16baaa', 'second' => '#ecf5ff'],
                    ['id' => '2', 'color' => '#36b368', 'second' => '#f0f9eb'],
                    ['id' => '3', 'color' => '#f6ad55', 'second' => '#fdf6ec'],
                    ['id' => '4', 'color' => '#f56c6c', 'second' => '#fef0f0'],
                    ['id' => '5', 'color' => '#4A5DFF', 'second' => '#4A5DFF'],
                ],
                'other' => [
                    'keepLoad' => '1200',
                    'autoHead' => false,
                    'footer' => false,
                ],
                'header' => [
                    'message' => '/admin/index/message/'
                ],
            ];
            $data['ex'] = [];
            $data['ex'][] = ['type' => 'cache', 'title' => '清除缓存'];
            $data['ex'][] = ['type' => 'info', 'title' => '个人资料'];
            $data['ex'][] = ['type' => 'pass', 'title' => '修改密码'];
            $data['ex'][] = ['type' => 'out', 'title' => '退出登录'];
            return BackCode($data);
        }
    }
}
